template.helper('formatCurrency',function(num){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return '￥' + (((sign)?'':'-') + num + '.' + cents);
});

template.helper('formatUnit',function(num, fixed){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = 0;

    var result = num;
    if(result>=(100*10000)){
    	result = (result/100/10000);
    	if(!isNaN(fixed) && result%1!==0){
    		result = result.toFixed(fixed);
    	}
    	result += "百万";
    } else if(result>=10000){
    	result = (result/10000);
    	if(!isNaN(fixed) && result%1!==0){
    		result = result.toFixed(fixed);
    	}
    	result += "万";
    }

    return result;
});

template.helper('dateFormat', function (date, format) {
    if(!isNaN(date)){
        date = new Date(parseInt(date)*parseInt(1000));
    } else{
        date = new Date(date);
    }
    var map = {
        "M": date.getMonth() + 1, //月份
        "d": date.getDate(), //日
        "h": date.getHours(), //小时
        "m": date.getMinutes(), //分
        "s": date.getSeconds(), //秒
        "q": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds() //毫秒
    };


    format = format.replace(/([yMdhmsqS])+/g, function(all, t){
        var v = map[t];
        if(v !== undefined){
            if(all.length > 1){
                v = '0' + v;
                v = v.substr(v.length-2);
            }
            return v;
        }
        else if(t === 'y'){
            return (date.getFullYear() + '').substr(4 - all.length);
        }
        return all;
    });
    return format;
});

template.helper('timeData', function (string) {
	var arr = string.split("T");
	datestring = arr[0];
    return datestring;
});
template.helper('timeDataMS', function (string) {
	var arr = string.split("T");
	var arr2 = arr[1].split(".");
	datestring = arr[0];
    return datestring+" "+arr2[0];
});
template.helper('stringData', function (json) {
    return JSON.stringify(json);
});
template.helper('escapeChinese',function(chinese){
    return encodeURIComponent(chinese);
});

template.helper('ticketType',function(money){

    if(isNaN(money)){money=0;}

    money = parseFloat(money);

    if(money<=100){
        return 'ste-1';
    } else if(money>100 && money<=200){
        return 'ste-2';
    } else if(money>200 && money<=500){
        return 'ste-3';
    } else if(money>500){
        return 'ste-4';
    }

    return '';
});

template.helper('formatCurLoc',function(i,pager,pageSize){
    return (i+((parseInt(pager)-1)*parseInt(pageSize)));
});

template.helper('Calc',function(num1,num2,type){
    if(type=='plus'){
        return parseFloat(num1)+parseFloat(num2);
    } else if (type=='reduce'){
        return parseFloat(num1)-parseFloat(num2);
    } else if(type=='multiply'){
        return parseFloat(num1)*parseFloat(num2);
    } else if(type=='divide'){
        return parseFloat(num1)/parseFloat(num2);
    }
});

template.helper('addImgUrlPrefix',function(url){
    return (Common.Config.ServerPath + url);
});

template.helper('formatHide',function(txt,beginLen,endLen,hideLen){
    if(isNaN(beginLen)){
        beginLen=1;
    }
    if(isNaN(endLen)){
        endLen=1;
    }
    if(isNaN(hideLen)){
        hideLen=3;
    }

    if(typeof(txt)=='undefined' || txt.length==1){
        return '***';
    }

    if(txt.length<=2){
        endLen=0;
    }

    if((beginLen>=1 || endLen>=1) && txt.length>(beginLen+endLen)){
        var hideStr="";
        for(var i=0;i<hideLen;i++){
            hideStr += "*";
        }
        return (txt.substr(0,beginLen)+hideStr+txt.substr(txt.length-endLen,txt.length));
    } else {
        return (txt.substr(0,1)+'***'+txt.substr(txt.length-1,txt.length));
    }
});

template.helper('compareTime',function(time1,type,time2){
    if(type=='now'){
        var ts = parseInt(Date.parse(new Date(time1)));
        if(ts>0){
            return true;
        }
        return false;
    }
});

template.helper('cutstr',function(str, len){
    if (typeof(str) === 'undefined') return;
    var str_length = 0;
    var str_len = 0;
    str_cut = new String();
    str_len = str.length;
    for (var i = 0; i < str_len; i++) {
        a = str.charAt(i);
        str_length++;
        if (escape(a).length > 4) {
            //中文字符的长度经编码之后大于4
            str_length++;
        }
        str_cut = str_cut.concat(a);
        if (str_length > len) {
            str_cut = str_cut.concat("...");
            return str_cut;
        }
    }
    //如果给定字符串小于指定长度，则返回源字符串；
    if (str_length <= len) {
        return str;
    }
});

template.helper('formatFixed',function(numberRound, roundDigit){
    if (numberRound >= 0) {
        var tempNumber = parseInt((numberRound * Math.pow(10, roundDigit) + 0.5)) / Math.pow(10, roundDigit);
        return tempNumber;
    } else {
        var numberRound1 = -numberRound;
        var tempNumber = parseInt((numberRound1 * Math.pow(10, roundDigit) + 0.5)) / Math.pow(10, roundDigit);
        return -tempNumber;
    }
});
